
# 三数之和 需要同时存在正数或者负数，或者全部为0


class Solution:

    def threeSum(self, nums:list):



        # 进行排序
        nums.sort()

        res = []
        for i in range(len(nums)):

            if nums[i] > 0:
                return res
            
            if nums[i - 1] == nums[i] and i > 0:
                continue
            
            l, r = i + 1, len(nums) - 1
    
        
            while(l < r):

                if nums[i] + nums[l] + nums[r] == 0:

                    res.append([nums[i], nums[l], nums[r]])
                    while(l < r and nums[l] == nums[l + 1]):
                        l += 1
                    
                    while(l < r and nums[r] == nums[r - 1]):
                        
                        r -= 1
                    
                    r -= 1
                    l += 1


                elif nums[i] + nums[l] + nums[r] > 0:
                    # 说明右边太高了
                    r -= 1
                else:
                    l += 1
            
        
        return res



